<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<pre>
#!/usr/bin/expect  -- 
####################################
# Copyright (C) 2005 by tencent
####################################

proc Usage_Exit {myself} {
	puts ""
	puts "### USAGE:  $myself ip user passwd destdir direction timeout"
	exit 1
}

if { [llength $argv] < 5 } {
	Usage_Exit $argv0
}

set ip [lindex $argv 0]
set user [lindex $argv 1]
set passwd [lindex $argv 2]
set destdir [lindex $argv 3]
set timeoutflag [lindex $argv 4]

set yesnoflag 0
set timeout $timeoutflag


for {} {1} {} {

# for is only used to retry when "Interrupted system call" occured

# scp2: -p Tells scp2 to preserve file attributes and timestamps
# scp2: -r Copy directories recursively.  Does not follow symbolic links
# scp2: -Q Do not show process indicator

# rsync: -a, --archive, archive mode, equivalent to -rlptgoD
# rsync: -r, --recursive, recurse into directories
# rsync: -t, --times, preserve times
# rsync: -z, --compress, compress file data
# rsync: --progress show progress during transfer

spawn sh rsync_cmd.exp $ip $user $passwd $destdir $timeout

expect 	{

	"assword:" {
		send "$passwd\r" 
		break;
	}
	
	"yes/no)?" {
		set yesnoflag 1
		send "yes\r"
		break;
	}
	
	"FATAL" {
		puts "\nCONNECTERROR: $ip occur FATAL ERROR!!!\n"
		exit 1
	}
	
	timeout {
		puts "\nCONNECTERROR: $ip logon TIMEOUT!!!\n"
		exit 1
	}
	
	"No route to host" {
		puts "\nCONNECTERROR: $ip No route to host!!!\n"
		exit 1
	}
	
	"Connection Refused" {
		puts "\nCONNECTERROR: $ip Connection Refused!!!\n"
		exit 1
	}

	"Connection refused" {
		puts "\nCONNECTERROR: $ip Connection Refused!!!\n"
		exit 1
	}

	"Host key verification failed" {
		puts "\nCONNECTERROR: $ip Host key verification failed!!!\n"
		exit 1
	}
	
	"Illegal host key" {
		puts "\nCONNECTERROR: $ip Illegal host key!!!\n"
		exit 1
	}
		
	"Connection Timed Out" {
		puts "\nCONNECTERROR: $ip logon TIMEOUT!!!\n"
		exit 1
	}

	"Interrupted system call" {
		puts "\n$ip Interrupted system call!!!\n"
	}
}

}

if { $yesnoflag == 1 } {
	expect {
		"assword:" {
			send "$passwd\r"
		}

		"yes/no)?" {
			set yesnoflag 2
			send "yes\r"
		}
	}
}

if { $yesnoflag == 2 } {
	expect {
		"assword:" {
			send "$passwd\r"
		}
	}
}

expect {
	"assword:" {
		send "$passwd\r"
		puts "\nPASSWORDERROR: $ip PASSWORD ERROR!!!\n"
		exit 1
	}
	
	eof { 
		puts "ABS_OK_GET_FILES: $ip\n"
		exit 0;
	}
}


</pre>
</body>
</html>